package redis

import (
	"context"
	"encoding/json"
	"errors"
	"gorm.io/gorm"
	"onerk4/zg5yk/ordering/common/dao/mysql"
	"onerk4/zg5yk/ordering/common/global"
	"onerk4/zg5yk/ordering/common/model"
	"time"
)

func QueryShowTakeoutList(query *gorm.DB) ([]*model.Takeout, error) {
	key := "list"
	result, err := global.Rdb.Get(context.Background(), key).Result()
	if err == nil {
		var list []*model.Takeout
		err := json.Unmarshal([]byte(result), &list)
		if err != nil {
			return nil, errors.New("反解析失败")
		}
		return list, nil
	}

	list, err := mysql.QueryTakeoutList(query)
	if err != nil {
		return nil, err
	}
	marshal, err := json.Marshal(list)
	if err != nil {
		return nil, errors.New("解析失败")
	}
	err = global.Rdb.Set(context.Background(), key, marshal, time.Second*60).Err()
	if err != nil {
		return nil, errors.New("存入失败")
	}
	return nil, errors.New("已存入redis缓存")
}
